#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <bus/tsi/tsi.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#else
#include <api/libpub/lib_frontend.h>
#endif
#include <api/libosd/osd_lib.h>

#ifdef AUTO_OTA
#include <api/libtsi/si_types.h>
#endif

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"
#include <api/libchunk/chunk.h>
#include <sys_parameters.h>
#include "win_ota_set.h"
#include "win_signal.h"
#ifdef SUPPORT_VMX
#include "vmx_ap/Osm.h"
#endif
#ifdef SUPPORT_VMX_STD	
#include "vmx_ap_std/Osm.h"
#endif
#ifdef SUPPORT_CAS_A
#include "win_com_menu_define.h"
#endif

#define OTA_DATA_SAT_POS    	192
#define OTA_DATA_TP_FREQ    	12604
#define OTA_DATA_TP_SYMB    	22000
#define OTA_DATA_TP_POL     	0/* H */
#define OTA_DATA_MODULATION      64
#define OTA_DATA_TP_BW     	0/* 6M */
#define OTA_DATA_TP_FREQ_T    	545000
#if defined(SUPPORT_VMX_STD) || defined(SUPPORT_VMX)
#define OTA_DATA_PID			OPERATOR_OTA_PID
#else
#define OTA_DATA_PID 			0x1A0 // 0x1A0=416, 1277=0x4FD
#endif
#define OTA_TUNER_IDX TUNER_EITHER_SELECT//TUNER_FIRST

#define DEB_PRINTF PRINTF
/*******************************************************************************
*	Objects definition
*******************************************************************************/

extern CONTAINER win_otaupg_con;

//OTA upgrade setting is different between DVBC and DVBS
//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
extern CONTAINER wota_con0;   /* SAT */
extern CONTAINER wota_con1;   /* TP Index */

extern TEXT_FIELD wota_txt0;
extern TEXT_FIELD wota_txt1;

extern TEXT_FIELD wota_line0;
extern TEXT_FIELD wota_line1;

extern MULTISEL		wota_num0;
extern MULTISEL		wota_num1;
//#endif

extern CONTAINER wota_con2;   /* Freq */
extern CONTAINER wota_con3;   /* Symb */
extern CONTAINER wota_con4;   /* Pol */
extern CONTAINER wota_con5;   /* PID */

extern TEXT_FIELD wota_txt2;
extern TEXT_FIELD wota_txt3;
extern TEXT_FIELD wota_txt4;
extern TEXT_FIELD wota_txt5;
extern TEXT_FIELD wota_txt6; /*Start*/

extern TEXT_FIELD wota_line2;
extern TEXT_FIELD wota_line3;
extern TEXT_FIELD wota_line4;
extern TEXT_FIELD wota_line5;
extern TEXT_FIELD wota_line6; 

extern EDIT_FIELD	wota_num2;
extern EDIT_FIELD   wota_num3;
extern MULTISEL    	wota_num4;
extern EDIT_FIELD   wota_num5;

static VACTION  otaset_btn_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT  otaset_btn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION  otaset_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT  otaset_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION  otaset_item_edf_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT  otaset_item_edf_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION  otaset_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT  otaset_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION  otaset_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT  otaset_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
void ota_set_front_type();//add by Iris for AUTO OTA

#ifdef	_BUILD_OTA_E_
#define WIN_SH_IDX	  	WSTL_WINSEARCH_03_HD
#else
#define WIN_SH_IDX	  	WSTL_WIN_BODYRIGHT_01_HD
#endif

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXT_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define SEL_SH_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_HL_IDX   WSTL_BUTTON_04_HD
#define SEL_SL_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_GRY_IDX  WSTL_BUTTON_07_HD

#define EDF_SH_IDX   WSTL_BUTTON_01_FG_HD
#define EDF_HL_IDX   WSTL_BUTTON_04_HD
#define EDF_SL_IDX   WSTL_BUTTON_01_FG_HD
#define EDF_GRY_IDX  WSTL_BUTTON_07_HD

#ifndef SD_UI
#if Maker_GUI_ON
#ifdef	_BUILD_OTA_E_
#define	W_W         Maker_GUI_SM_W             //493
#define	W_H         Maker_GUI_SM_H             //320
#define	W_L         Maker_GUI_SM_L  //60
#define	W_T         Maker_GUI_SM_T              //GET_MID_T(W_H)//60
#define CON_L		(W_L + 24)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 48)
#else

#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	Maker_GUI_SM_L
#define	W_T     	Maker_GUI_SM_T
#define	W_W     	Maker_GUI_SM_W
#define	W_H     	Maker_GUI_SM_H
#endif

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)
#endif
#else
#ifdef	_BUILD_OTA_E_
#define	W_W         692             //493
#define	W_H         488             //320
#define	W_L         GET_MID_L(W_W)  //60
#define	W_T         98              //GET_MID_T(W_H)//60
#define CON_L		(W_L + 24)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 48)
#else

#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)
#endif
#endif
//#define CON_L		W_L


#define CON_H		40
#define CON_GAP		8

#define TXT_L_OF  	10
#define TXT_W  		260
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+2)
#define LINE_W		CON_W
#if Maker_GUI_ON
#define LINE_H     	0
#define LIST_ITEMCNT 5
#else
#define LINE_H     	2
#define LIST_ITEMCNT 7
#endif
#else
#ifdef	_BUILD_OTA_E_
#define	W_W         482             //493
#define	W_H         360             //320
#define	W_L         GET_MID_L(W_W)  //60
#define	W_T         60              //GET_MID_T(W_H)//60
#define CON_L		(W_L + 24)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 40)
#else
#define	W_L     	105//384
#define	W_T     	57//138
#define	W_W     	482
#define	W_H     	370
#define CON_L		(W_L+20)
#define CON_T		(W_T + 8)
#define CON_W		(W_W - 40)
#endif

#define CON_H		30
#define CON_GAP		8

#define TXT_L_OF  	10
#define TXT_W  		180
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+2)
#define LINE_W		CON_W
#define LINE_H     	2
#define LIST_ITEMCNT 7

#endif

UINT16 otaset_display_strs[3][30];

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,hl,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,hl,CON_SL_IDX,CON_GRY_IDX,   \
    otaset_item_keymap,otaset_item_callback,  \
    conobj, ID,1)
    
#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_TXT_BTN(root,varTxt,nextObj,varLine,ID,IDu,IDd,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,&root,nextObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    otaset_btn_keymap,otaset_btn_callback,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, TXT_L_OF,0,resID,NULL)\
	LDEF_LINE(&root,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF + 10,LINE_W,LINE_H,0,0)

#define LDEF_MSEL(root, varNum, nxtObj, ID, l, t, w, h,style,cur,cnt,ptabl)	\
    DEF_MULTISEL(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, SEL_SH_IDX,SEL_HL_IDX,SEL_SL_IDX,SEL_GRY_IDX,   \
    otaset_item_sel_keymap,otaset_item_sel_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 30,0,style,ptabl,cur,cnt)
    
#define LDEF_EDIT(root, varNum, nxtObj, ID, l, t, w, h,style,cursormode,pat,pre,sub,str)	\
    DEF_EDITFIELD(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, EDF_SH_IDX,EDF_HL_IDX,EDF_SL_IDX,EDF_GRY_IDX,   \
    otaset_item_edf_keymap,otaset_item_edf_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 30,0,style,pat,10,cursormode,pre,sub,str)

#define LDEF_MENU_ITEM_SEL(root,varCon,nxtObj,varTxt,varNum,varLine,ID,IDu,IDd,\
						l,t,w,h,resID,style,cur,cnt,ptbl) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,CON_HL_IDX,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_MSEL(&varCon,varNum,NULL/*&varLine*/   ,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,cur,cnt,ptbl)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_MENU_ITEM_EDF(root,varCon,nxtObj,varTxt,varNum,varLine,ID,IDu,IDd,\
						l,t,w,h,resID,style,cursormode,pat,pre,sub,str) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,CON_L,t,CON_W,CON_H,CON_HL_IDX,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_EDIT(&varCon,varNum,NULL/*&varLine*/,ID,l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,cursormode,pat,pre,sub,str)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)


#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    otaset_keymap,otaset_callback,  \
    nxtObj, focusID,0)


extern char fre_pat[];
extern char symb_pat[];
extern UINT16 fre_sub[10];
extern UINT16 symb_sub[10];
extern UINT16 tp_polarity_ids[];

extern char pid_pat[];


//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)

#define EDIT_TP_SATE_ID     1
#define EDIT_TP_TPIDX_ID    2
#define EDIT_TP_FREQ_ID     3
#define EDIT_TP_SYMB_ID     4
#define EDIT_TP_POL_ID      5
#define EDIT_TP_PID_ID  	  6
#define EDIT_TP_START_ID   7

LDEF_MENU_ITEM_SEL(win_otaupg_con,wota_con0,&wota_con1,wota_txt0,wota_num0,wota_line0,1,7,2,\
			CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_INFO_SATELLITE, STRING_PROC, 0, 0, NULL)

LDEF_MENU_ITEM_SEL(win_otaupg_con,wota_con1,&wota_con2,wota_txt1,wota_num1,wota_line1,2,1,3,\
			CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_INFO_TRANSPONDER, STRING_PROC, 0, 0, NULL)

LDEF_MENU_ITEM_EDF(win_otaupg_con,wota_con2,&wota_con3,wota_txt2,wota_num2,wota_line2,3,2,4,\
			CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_DISPLAY_FREQUENCY, \
			NORMAL_EDIT_MODE, CURSOR_SPECIAL, fre_pat, NULL,fre_sub,otaset_display_strs[0])

LDEF_MENU_ITEM_EDF(win_otaupg_con,wota_con3,&wota_con4,wota_txt3,wota_num3,wota_line3,4,3,5,\
			CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, RS_INFO_SYMBOL, \
			NORMAL_EDIT_MODE, CURSOR_SPECIAL, symb_pat, NULL,symb_sub,otaset_display_strs[1])

LDEF_MENU_ITEM_SEL(win_otaupg_con,wota_con4,&wota_con5,wota_txt4,wota_num4,wota_line4,5,4,6,\
			CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_INFO_POLARITY, STRING_ID, 0, 2, tp_polarity_ids)

LDEF_MENU_ITEM_EDF(win_otaupg_con,wota_con5,&wota_txt6,wota_txt5,wota_num5,wota_line5,6,5,7,\
			CON_L, CON_T + (CON_H + CON_GAP)*5, CON_W, CON_H, RS_UPGRADE_PID, \
			NORMAL_EDIT_MODE, CURSOR_NO, pid_pat, NULL,NULL,otaset_display_strs[2])
			
LDEF_TXT_BTN(win_otaupg_con,wota_txt6,&wota_line6, wota_line6, 7,6,1,\
			CON_L, CON_T + (CON_H + CON_GAP)*6, CON_W, CON_H,RS_COMMON_START)

LDEF_WIN(win_otaupg_con,&wota_con0,W_L, W_T,W_W,W_H,1)

#define SAT_NUM  wota_num0.nCount
#define SAT_POS  wota_num0.nSel

#define TP_NUM    wota_num1.nCount
#define TP_POS    wota_num1.nSel

#define DVBC_OTA_EDIT_LEN  0x06 // add .xx

//#else
/*
#define EDIT_TP_FREQ_ID     1
#define EDIT_TP_SYMB_ID     2
#define EDIT_TP_POL_ID      3
#define EDIT_TP_PID_ID  	4
#define EDIT_TP_START_ID    5

#define DVBC_OTA_EDIT_LEN  0x06 // add .xx

static UINT16 ota_model_type_ids[] =
{
    RS_CONSTELLATION_16,
    RS_CONSTELLATION_32,
    RS_CONSTELLATION_64,
    RS_CONSTELLATION_128,
    RS_CONSTELLATION_256,
};
static char dvbc_fre_pat[]  = "f32";
static char dvbc_symb_pat[]  = "s5";

LDEF_MENU_ITEM_EDF(win_otaupg_con,wota_con2,&wota_con3,wota_txt2,wota_num2,wota_line2,1,5,2,\
			CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_DISPLAY_FREQUENCY, \
			NORMAL_EDIT_MODE, CURSOR_SPECIAL, dvbc_fre_pat, NULL,fre_sub,otaset_display_strs[0])

LDEF_MENU_ITEM_EDF(win_otaupg_con,wota_con3,&wota_con4,wota_txt3,wota_num3,wota_line3,2,1,3,\
			CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_INFO_SYMBOL, \
			NORMAL_EDIT_MODE, CURSOR_SPECIAL, dvbc_symb_pat, NULL,symb_sub,otaset_display_strs[1])

LDEF_MENU_ITEM_SEL(win_otaupg_con,wota_con4,&wota_con5,wota_txt4,wota_num4,wota_line4,3,2,4,\
			CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_INSTALLATION_QAM_MODE, STRING_ID, 0, 5, ota_model_type_ids)

LDEF_MENU_ITEM_EDF(win_otaupg_con,wota_con5,&wota_txt6,wota_txt5,wota_num5,wota_line5,4,3,5,\
			CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, RS_UPGRADE_PID, \
			NORMAL_EDIT_MODE, CURSOR_NO, pid_pat, NULL,NULL,otaset_display_strs[2])
			
LDEF_TXT_BTN(win_otaupg_con,wota_txt6,&wota_line6, wota_line6, 5,4,1,\
			CON_L, CON_T + (CON_H + CON_GAP)*6, CON_W, CON_H,RS_COMMON_START)

LDEF_WIN(win_otaupg_con,&wota_con2,W_L, W_T,W_W,W_H,1)
*/

//#endif

static UINT16 ota_model_type_ids[] =
{
    RS_CONSTELLATION_16,
    RS_CONSTELLATION_32,
    RS_CONSTELLATION_64,
    RS_CONSTELLATION_128,
    RS_CONSTELLATION_256,
};

static char fre_pat_dvbt[]	= "r100000~858000";
static char symb_pat_dvbt[]  = "r1000~45000";

static char fre_pat_dvbc[]  = "f32";
static char symb_pat_dvbc[]  = "s5";

static UINT16 bandwidth_ids[] = 
{
	RS_6MHZ,
	RS_7MHZ,
	RS_8MHZ,
};

POBJECT_HEAD otaset_items[]=
{
    (POBJECT_HEAD)&wota_con0,
    (POBJECT_HEAD)&wota_con1,
    (POBJECT_HEAD)&wota_con2,
    (POBJECT_HEAD)&wota_con3,
    (POBJECT_HEAD)&wota_con4,
    (POBJECT_HEAD)&wota_con5,
    (POBJECT_HEAD)&wota_txt6
};


typedef struct
{
    OSD_RECT  obj_rect;
    UINT32    obj_active;
    UINT32    obj_strid;
    
}OTASET_OBJECT_ATTR;

OTASET_OBJECT_ATTR  otaset_dvbs_menu[]= 
{
    {{CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H},C_ATTR_ACTIVE,RS_INFO_SATELLITE},
    {{CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H},C_ATTR_ACTIVE,RS_INFO_TRANSPONDER},
    {{CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H},C_ATTR_ACTIVE,RS_DISPLAY_FREQUENCY},
    {{CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H},C_ATTR_ACTIVE,RS_INFO_SYMBOL},        
    {{CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H},C_ATTR_ACTIVE,RS_INFO_POLARITY},
    {{CON_L, CON_T + (CON_H + CON_GAP)*5, CON_W, CON_H},C_ATTR_ACTIVE,RS_UPGRADE_PID},
    {{CON_L, CON_T + (CON_H + CON_GAP)*6, CON_W, CON_H},C_ATTR_ACTIVE,RS_COMMON_START},
};

OTASET_OBJECT_ATTR  otaset_dvbt_menu[]=
{
    {{CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H},C_ATTR_HIDDEN,RS_INFO_SATELLITE},
    {{CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H},C_ATTR_HIDDEN,RS_INFO_TRANSPONDER},
    {{CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H},C_ATTR_ACTIVE,RS_DISPLAY_FREQUENCY},
    {{CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H},C_ATTR_HIDDEN,RS_INFO_SYMBOL},        
    {{CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H},C_ATTR_ACTIVE,RS_INSTALLATION_BANDWIDTH},
    {{CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H},C_ATTR_ACTIVE,RS_UPGRADE_PID},
    {{CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H},C_ATTR_ACTIVE,RS_COMMON_START},
};
OTASET_OBJECT_ATTR  otaset_dvbc_menu[]= 
{
    {{CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H},C_ATTR_HIDDEN,RS_INFO_SATELLITE},
    {{CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H},C_ATTR_HIDDEN,RS_INFO_TRANSPONDER},
    {{CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H},C_ATTR_ACTIVE,RS_DISPLAY_FREQUENCY},
    {{CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H},C_ATTR_ACTIVE,RS_INFO_SYMBOL},        
    {{CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H},C_ATTR_ACTIVE,RS_INSTALLATION_QAM_MODE},
    {{CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H},C_ATTR_ACTIVE,RS_UPGRADE_PID},
    {{CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H},C_ATTR_ACTIVE,RS_COMMON_START},
};

#define TP_POL    wota_num4.nSel

/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/
#define OTA_TURN_TIMER_NAME "otatn"
#define OTA_TURN_TIMER_TIME    600
static ID ota_turn_timer_id = OSAL_INVALID_ID;

char pid_pat[]  = "r32~8191";
//UINT16 otaset_display_strs[3][50];
UINT16 ota_presat_id,ota_satpos,ota_pol;//,ota_pid;

UINT32 ota_freq;
UINT32 ota_symb;
UINT32 ota_pid;
UINT16 ota_modulation;

UINT16 ota_bandwidth;

OTA_CFG m_ota_cfg;

static UINT32 cur_front_type = 0;

static void win_otaset_load_default_setting(BOOL checkonly,UINT32 fe_type);
static void win_otaset_load_default_setting_other(BOOL checkonly,UINT32 fe_type);

//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
  static void win_otaset_load_sat_setting(UINT32 sat_idx, BOOL update);
  static void win_otaset_load_tp_setting(UINT32 tp_idx, BOOL update);
//#endif  

static void win_otaset_load_FreqSymbPid_display(UINT32 fe_type);
static void win_otaset_load_FreqSymbPid_display_other(UINT32 fe_type);

static INT32 win_otaset_get_FreqSymbPid_display(UINT32 fe_type);
static INT32 win_otaset_get_FreqSymbPid_display_other(UINT32 fe_type);

void win_otaset_turn_frntend(void);
void win_otaset_turn_frntend_ex(UINT32 fe_type);
void win_otaset_menu_config(UINT32 fe_type);
void win_signal_refresh_combo(UINT32 fe_type);
#ifdef AUTO_OTA 
void win_auto_otaset_load_default_setting(BOOL checkonly,UINT32 fe_type);
static void win_auto_otaset_load_default_setting_other(BOOL checkonly,UINT32 fe_type);
#endif
static UINT8 find_ota_loader(UINT32 *ota_loader_addr)
{
	#define C_SECTOR_SIZE   0x10000
    UINT8 *buffer;
    UINT32 data_id;
    UINT32 data_len;
    UINT32 data_off;
    UINT32 data_crc;
    UINT32 crc;
    UINT32 pointer = 0;
	struct sto_device *flash_dev = (struct sto_device *)dev_get_by_type(NULL, HLD_DEV_TYPE_STO);

    while(pointer < (UINT32)flash_dev->totol_size)
    {
        data_id  = sto_fetch_long((UINT32)pointer + CHUNK_ID);
        data_len = sto_fetch_long((UINT32)pointer + CHUNK_LENGTH);
        data_off = sto_fetch_long((UINT32)pointer + CHUNK_OFFSET);
        data_crc = sto_fetch_long((UINT32)pointer + CHUNK_CRC);
		#ifdef _VMX_CA_STD_ENABLE_
		if(data_len && (data_len < data_off) && ((data_id & CHUNKID_SECLOADER_MASK) == CHUNKID_SECLOADER))
        {
              *ota_loader_addr = pointer;
              return 1;
        }
		#else
        if(data_len && (data_len < data_off) && (data_crc != NO_CRC) && ((data_id & CHUNKID_SECLOADER_MASK) == CHUNKID_SECLOADER))
        {
            buffer = (UINT8 *)MALLOC(data_len);
        	if(buffer != NULL)
        	{
    		    sto_get_data(flash_dev, buffer, pointer + 16, data_len);
                crc = MG_Table_Driven_CRC(0xFFFFFFFF, buffer, data_len);
                FREE(buffer);
                if(data_crc == crc)
                {
                    *ota_loader_addr = pointer;
                    return 1;
                }
        	}
        }
		#endif
	    pointer += C_SECTOR_SIZE;
    }
    return 0;
}

BOOL ota_load_parameter()
{
	UINT32 addr;
	UINT32 len;	
    unsigned char *p = NULL;
	UINT32 m_ota_load_offset;
	BOOL ret = FALSE;	
	UINT32 ota_bin_max_size = 0x200000;

	struct sto_device *flash_dev = (struct sto_device *)dev_get_by_type(NULL, HLD_DEV_TYPE_STO);

//#ifndef _CAS9_CA_ENABLE_
	do 
	{
//for 4M flash  ENABLE_4M_FLASH
#ifdef _CAS9_CA_ENABLE
#if !defined (_M3383_SABBAT_) &&  !defined (_ENABLE_4M_FLASH_)
		break;
#endif
#endif

		if(!find_ota_loader(&m_ota_load_offset))
			break;;

		if((UINT32)flash_dev->totol_size > m_ota_load_offset)
		{
			ota_bin_max_size = (UINT32)flash_dev->totol_size-m_ota_load_offset;
		}
		if(ota_bin_max_size < 0x100000)
			ota_bin_max_size = 0x100000;
		
		//chunk_init(m_ota_load_offset+SYS_FLASH_BASE_ADDR, ota_bin_max_size);
		sto_chunk_init(m_ota_load_offset, ota_bin_max_size);
		
		// get user_db address
		//if(!get_chunk_add_len(0x11EE0200, &addr, &len))
		if(!api_get_chunk_add_len(0x11EE0200, &addr, &len))
			break;

		//p = (UINT8 *)addr;
		
		sto_get_data(flash_dev, (UINT8*)&m_ota_cfg, addr, sizeof(m_ota_cfg));
		//MEMCPY(&m_ota_cfg, p, sizeof(m_ota_cfg));
	    
        cur_front_type = m_ota_cfg.fe_type;

        if(FRONTEND_TYPE_S == cur_front_type|| FRONTEND_TYPE_C == cur_front_type)
        {
        
            ota_freq = m_ota_cfg.t_node.frq;
            ota_symb = m_ota_cfg.t_node.sym;
            ota_pid = m_ota_cfg.pid;
            ota_pol = m_ota_cfg.t_node.pol;
            ota_modulation= m_ota_cfg.t_node.FEC_inner; //DVBC 
            ret = TRUE;
        }
        else if(FRONTEND_TYPE_T == cur_front_type|| FRONTEND_TYPE_ISDBT == cur_front_type)
        {
            ota_freq = m_ota_cfg.t_node.frq;
            ota_pid = m_ota_cfg.pid;
            ota_bandwidth = m_ota_cfg.t_node.bandwidth - 6;
            ret = TRUE;
        }        
        
	}while(0);
//#endif

	if(!ret && (ota_freq == 0) && (ota_pid == 0))
	{
/*
//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
		S_NODE *p_s_node = &m_ota_cfg.s_node;
		p_s_node->lnb_low = 5150;
		p_s_node->lnb_high= 5150;
		p_s_node->DiSEqC_port = 4;
		p_s_node->tuner1_valid = 1;
        
	    ota_freq = 4150;
	    ota_symb = 40000;
	    ota_pid = 256;
		ota_pol = 0;
//#else
        //DVBC setting
	    ota_freq = 5050;
	    ota_symb = 6875;
	    ota_pid = OTA_DATA_PID;
		ota_modulation = QAM64;
//#endif
*/
	}

/*
#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
	OSD_ChangeFocus((POBJECT_HEAD)&win_otaupg_con, 7, 0);
	CONTAINER *con = &wota_con0;
	OSD_SetAttr(con, C_ATTR_INACTIVE);
	con = &wota_con1;
	OSD_SetAttr(con, C_ATTR_INACTIVE);		
	PTEXT_FIELD pText = &wota_txt0;
	OSD_SetAttr(pText, C_ATTR_INACTIVE);
	pText = &wota_txt1;
	OSD_SetAttr(pText, C_ATTR_INACTIVE);		

	PMULTISEL pmul = &wota_num0;
	OSD_SetAttr(pmul, C_ATTR_INACTIVE);	
	pmul = &wota_num1;
	OSD_SetAttr(pmul, C_ATTR_INACTIVE);
#endif
*/
	return ret;
}

void ota_save_parameter(UINT8 *p)
{
    unsigned long crc, len;

    m_ota_cfg.fe_type = cur_front_type;
    
	MEMCPY(p+CHUNK_HEADER_SIZE, &m_ota_cfg, sizeof(m_ota_cfg));
	MG_Setup_CRC_Table();
    len = fetch_long(p + CHUNK_LENGTH);
	crc = (unsigned long)MG_Table_Driven_CRC(0xFFFFFFFF, p + CHUNK_NAME, len);
	store_long( (unsigned char*)(p + CHUNK_CRC), crc);
}

static void win_ota_tpturn_handler(UINT32 nouse)
{
	if(ota_turn_timer_id != OSAL_INVALID_ID)
	{
		ota_turn_timer_id = OSAL_INVALID_ID;
		ap_send_msg(CTRL_MSG_SUBTYPE_CMD_TP_TURN,(UINT32)&win_otaupg_con,FALSE);	
	}
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION  otaset_btn_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
	case V_KEY_ENTER:
	case V_KEY_RIGHT:		
		act = VACT_ENTER;
		break;
	default:
		act = VACT_PASS;
	}

	return act;
}

static PRESULT  otaset_btn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	POBJECT_HEAD pmenu;
	
	switch(event)
	{
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		if(unact == VACT_ENTER)
		{
			win_otaset_get_FreqSymbPid_display(cur_front_type);

			pmenu = (POBJECT_HEAD)&g_win_otaupg ;
			if(OSD_ObjOpen(pmenu, MENU_OPEN_TYPE_MENU + EDIT_TP_START_ID) != PROC_LEAVE)
				menu_stack_push(pmenu);

			ret = PROC_LOOP;
		}
		break;
	}

	return ret;
}

#define VACT_POP_UP	(VACT_PASS + 1)

static VACTION  otaset_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
  	case V_KEY_LEFT:
        act = VACT_DECREASE;
        break;
    case V_KEY_RIGHT:
        act = VACT_INCREASE;
		break;
	case V_KEY_ENTER:
		act = VACT_POP_UP;
		break;
	default:
		act = VACT_PASS;
	}

	return act;	
}

static PRESULT  otaset_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 bID;
	UINT32 sel;
	UINT16 * Uni_str;
	S_NODE s_node;
	VACTION unact;
	COM_POP_LIST_TYPE_T list_type;
	OSD_RECT rect;
	COM_POP_LIST_PARAM_T param;
	UINT16 cur_idx;

	bID = OSD_GetObjID(pObj);

	switch(event)
	{
	case EVN_REQUEST_STRING:
		sel = param1;
		Uni_str= (UINT16*)param2;
    #if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
		if(bID == EDIT_TP_SATE_ID)
		{
		#ifdef	_BUILD_OTA_E_
			ComAscStr2Uni("Fixed Sat", Uni_str);
		#else
			get_tuner_sat_name(OTA_TUNER_IDX,sel,Uni_str);
		#endif
		}
		else if(bID == EDIT_TP_TPIDX_ID)
		{
			get_tuner_sat(OTA_TUNER_IDX,SAT_POS,&s_node);
		#ifdef	_BUILD_OTA_E_
			ComAscStr2Uni("Fixed TP", Uni_str);
		#else
			get_tp_name(s_node.sat_id,sel,Uni_str);
		#endif
		}
    #endif
    break;
	case EVN_POST_CHANGE:
		sel = param1;
        
    #if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
		if(bID == EDIT_TP_SATE_ID)
			win_otaset_load_sat_setting(sel,TRUE);
		else if(bID == EDIT_TP_TPIDX_ID)
			win_otaset_load_tp_setting(sel,TRUE);
    #endif        
		api_stop_timer(&ota_turn_timer_id);
		ota_turn_timer_id = api_start_timer(OTA_TURN_TIMER_NAME, OTA_TURN_TIMER_TIME, win_ota_tpturn_handler); 	
		//win_otaset_turn_frntend();
		break;
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);		
		if(unact ==  VACT_POP_UP)
		{
    #if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
			ret = PROC_LOOP;
			OSD_SetRect2(&rect,&pObj->frame);
			param.selecttype = POP_LIST_SINGLESELECT;
			switch(bID)
			{
			case EDIT_TP_SATE_ID:
				rect.uLeft -= 40;
				rect.uWidth += 40;
				list_type = POP_LIST_TYPE_SAT;
				rect.uHeight = 300;//230;
				param.id = OTA_TUNER_IDX;
				param.cur = SAT_POS;
				break;
			case EDIT_TP_TPIDX_ID:
				list_type = POP_LIST_TYPE_TP;
				rect.uHeight = 300;//230;
				get_tuner_sat(OTA_TUNER_IDX,SAT_POS,&s_node);
				param.id = s_node.sat_id;
				param.cur = TP_POS;
				break;
			default:
				list_type = 0xFF;
				break;
			}

			if(list_type == 0xFF)
				break;

			cur_idx = win_com_open_sub_list(list_type,&rect,&param);
			if(cur_idx == LIST_INVALID_SEL_IDX || cur_idx == param.cur)
				break;

			if(bID == EDIT_TP_SATE_ID)
			{
				SAT_POS = cur_idx;
				OSD_TrackObject((POBJECT_HEAD)&wota_con0, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);											
				win_otaset_load_sat_setting((UINT32)cur_idx, TRUE);
			}
			else if(bID == EDIT_TP_TPIDX_ID)
			{
				TP_POS = cur_idx;
				OSD_TrackObject((POBJECT_HEAD)&wota_con1, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);	
				win_otaset_load_tp_setting((UINT32)cur_idx,TRUE);
			}
    #endif
			api_stop_timer(&ota_turn_timer_id);
			ota_turn_timer_id = api_start_timer(OTA_TURN_TIMER_NAME, OTA_TURN_TIMER_TIME, win_ota_tpturn_handler);
			//win_otaset_turn_frntend();
		
		}
	}
	
	return ret;
}

static VACTION  otaset_item_edf_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
    {
	case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
	case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
	case V_KEY_8:	case V_KEY_9:
		act = key - V_KEY_0 + VACT_NUM_0;
		break;
	case V_KEY_LEFT:
		act = VACT_EDIT_LEFT;
		break;
	case V_KEY_RIGHT:
		act = VACT_EDIT_RIGHT;
		break;		
	default:					
		act = VACT_PASS;
	}

	return act;	 
}

static PRESULT  otaset_item_edf_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 bID;
	UINT8 status;
	EDIT_FIELD* edf;

	bID = OSD_GetObjID(pObj);

	edf = (EDIT_FIELD*)pObj;

	switch(event)
	{
	case EVN_POST_CHANGE:
		status = OSD_GetEditFieldStatus(edf);
		if(status == SELECT_STATUS)
		{
			if(bID == EDIT_TP_FREQ_ID || bID == EDIT_TP_SYMB_ID)
			{
				api_stop_timer(&ota_turn_timer_id);
				ota_turn_timer_id = api_start_timer(OTA_TURN_TIMER_NAME, OTA_TURN_TIMER_TIME, win_ota_tpturn_handler);
				//win_otaset_turn_frntend();
			}
		}
		break;
	case EVN_PARENT_FOCUS_POST_LOSE:
		if(bID == EDIT_TP_FREQ_ID || bID == EDIT_TP_SYMB_ID)
		{
			if(win_otaset_get_FreqSymbPid_display(cur_front_type) != 0)
			{
				api_stop_timer(&ota_turn_timer_id);
				ota_turn_timer_id = api_start_timer(OTA_TURN_TIMER_NAME, OTA_TURN_TIMER_TIME, win_ota_tpturn_handler);
				//win_otaset_turn_frntend();
			}
		}			
			
		
		break;
		
	}

	return ret;
}

static VACTION  otaset_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;

	return act;	
}

static PRESULT  otaset_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;

	return ret;
}

static VACTION  otaset_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
  	case V_KEY_UP:
        act = VACT_CURSOR_UP;
        break;
    case V_KEY_DOWN:
        act = VACT_CURSOR_DOWN;
		break;	
	case V_KEY_EXIT:
	case V_KEY_MENU:
#ifdef	_BUILD_OTA_E_		
		act = VACT_PASS;
#else
		act = VACT_CLOSE;
#endif
		break;
	default:
		act = VACT_PASS;
	}

	return act;	
}

#if defined(SUPPORT_VMX_STD) || defined(SUPPORT_VMX)
BOOL win_otaset_osm_setting(UINT8 Tp_pos, UINT32 fe_type)
{
	UINT8 	tuner_sat_pos;	
	UINT32 	tuner_sat_num;
	S_NODE s_node;
	T_NODE t_node;
	INT32 ret;
	UINT32 tp_pos,tp_cnt;
	OSM_TRIGGERS* pOSM_Triggers=GetOsmTriggers();


#if (defined DVBS_SUPPORT)
	if(pOSM_Triggers->OTA_TP_Num==0 && pOSM_Triggers->OTA_TP[0].frequency==0 && pOSM_Triggers->OTA_TP[0].symbol==0)
	{
		//libc_printf("undefined TP, use default upgrade parameters\n");
		return FALSE;
	}
	
	tuner_sat_num = get_tuner_sat_cnt(TUNER_EITHER_SELECT);	
	//libc_printf("tuner_sat_num=%d\n",tuner_sat_num);
	for(tuner_sat_pos=0;tuner_sat_pos<tuner_sat_num;tuner_sat_pos++)
	{
		get_tuner_sat(TUNER_EITHER_SELECT,tuner_sat_pos,&s_node);
	#if 0	//debug message
		UINT8	name[64];
		ComUniStrToAsc(s_node.sat_name, name);
		libc_printf("tuner_sat_pos=%d,sat_name=%s,sat_orbit=%d,sat_id=%d\n",tuner_sat_pos,name,s_node.sat_orbit,s_node.sat_id);
	#endif
		if(s_node.sat_orbit == pOSM_Triggers->OTA_TP[Tp_pos].orbital)
		{
			break;
		}
	}
	
	if(tuner_sat_pos==tuner_sat_num)
	{
		//libc_printf("sat not found ,TP.orbital=%d\n",pOSM_Triggers->OTA_TP[Tp_pos].orbital);
		return FALSE;
	}

	recreate_tp_view(VIEW_SINGLE_SAT, s_node.sat_id);
	tp_cnt = get_tp_num_sat(s_node.sat_id);
	for(tp_pos=0;tp_pos<tp_cnt;tp_pos++)
	{
		get_tp_at(s_node.sat_id, tp_pos, &t_node);
		if(t_node.frq == pOSM_Triggers->OTA_TP[Tp_pos].frequency && t_node.sym == pOSM_Triggers->OTA_TP[Tp_pos].symbol && t_node.pol == pOSM_Triggers->OTA_TP[Tp_pos].pol)
		{
			break;
		}
	}

	if(tp_pos==tp_cnt)	//not found tp
	{
		MEMSET(&t_node, 0, sizeof(T_NODE));

		t_node.frq = pOSM_Triggers->OTA_TP[Tp_pos].frequency;
		t_node.sym = pOSM_Triggers->OTA_TP[Tp_pos].symbol;
		t_node.pol = pOSM_Triggers->OTA_TP[Tp_pos].pol;
		t_node.sat_id = s_node.sat_id;
		ret = add_node(TYPE_TP_NODE,s_node.sat_id,(void*)&t_node);
		if(ret !=SUCCESS)
		{
			//libc_printf("create tp failed ret=%d\n",ret);
			return FALSE;
		}
		else
		{
			update_data();
		}
	}
	SAT_NUM	= tuner_sat_num;
	SAT_POS	= tuner_sat_pos;
	TP_NUM	= tp_cnt;
	TP_POS	= tp_pos;
	
	//libc_printf("rescan tp_id=%d,frq=%d,sym=%d,pol=%d,sat_id=%d\n",t_node.tp_id,t_node.frq,t_node.sym,t_node.pol,t_node.sat_id);
	ota_freq 	= t_node.frq;
	ota_symb 	= t_node.sym;
	ota_pol 	= t_node.pol;
	ota_pid 	= pOSM_Triggers->OTA_PID;
#elif (defined DVBC_SUPPORT)
	if(pOSM_Triggers->OTA_TP_Num==0 && pOSM_Triggers->OTA_TP[0].cab_frequency==0 && pOSM_Triggers->OTA_TP[0].cab_symbol_rate==0)
	{
		//libc_printf("undefined TP, use default upgrade parameters\n");
		return FALSE;
	}

	ota_freq = pOSM_Triggers->OTA_TP[Tp_pos].cab_frequency;
    	ota_symb = pOSM_Triggers->OTA_TP[Tp_pos].cab_symbol_rate;
    	ota_modulation= QAM16 + (pOSM_Triggers->OTA_TP[Tp_pos].cab_QAM - 1);
    	ota_pid = pOSM_Triggers->OTA_PID;
	
#endif

	return TRUE;
}
#endif

#ifdef NEW_MANUAL_OTA
BOOL ota_set_menu;
#endif
static PRESULT  otaset_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT32 sat_cnt;
    POBJECT_HEAD    pfocusobj;
	#ifdef NEW_MANUAL_OTA
	date_time et;
	UINT8 back_saved;			
	BOOL flag_et = api_ota_get_recently_timer(&et);
	BOOL bootup = FALSE;
	#endif

	switch(event)
	{
	case EVN_PRE_OPEN:

		ota_presat_id = 0xFFFF;
 		//wincom_open_title(pObj,RS_UPGRADE_MODE_SAT, 0);
 		wincom_open_title(pObj,RS_TOOLS_SW_UPGRADE_BY_OTA, 0);
#ifdef	_BUILD_OTA_E_
		ota_load_parameter();
#else
        if((MENU_OPEN_TYPE_STACK & param2) != MENU_OPEN_TYPE_STACK)
        {
            cur_front_type = param2 & 0x000000ff;
        }
		#if defined(SUPPORT_VMX_STD) || defined(SUPPORT_VMX)
		extern UINT8 osm_cur_tp;
		if(win_otaset_osm_setting(osm_cur_tp, cur_front_type)==FALSE)
	#endif
		{
		win_otaset_load_default_setting(0,cur_front_type);
		}	
#endif
        
//#if defined(_BUILD_OTA_E_) && defined(DVBC_SUPPORT)
	//cur_front_type  = FRONTEND_TYPE_C;
#if defined(SUPPORT_VMX_STD)
	cur_front_type  = FRONTEND_TYPE_C;
#endif

#if defined(SUPPORT_VMX) && defined(DVBC_SUPPORT)
	cur_front_type  = FRONTEND_TYPE_C;
#endif
//#endif
        win_otaset_menu_config(cur_front_type);
        pfocusobj = OSD_GetFocusObject(pObj);
        if(pfocusobj != NULL)
        {            
            if(pfocusobj->bAttr != C_ATTR_ACTIVE)
            {
                pfocusobj = OSD_GetAdjacentObject(pfocusobj,VACT_CURSOR_DOWN);
                if(pfocusobj != NULL)
                    OSD_ChangeFocus(pObj,pfocusobj->bID,0);
            }
        }
        #ifndef NEW_MANUAL_OTA
        win_otaset_load_FreqSymbPid_display(cur_front_type);
        #else
		remove_menu_item((CONTAINER *)pObj,(POBJECT_HEAD)&wota_con2,CON_H + CON_GAP);
		remove_menu_item((CONTAINER *)pObj,(POBJECT_HEAD)&wota_con4,CON_H + CON_GAP);
		remove_menu_item((CONTAINER *)pObj,(POBJECT_HEAD)&wota_con5,CON_H + CON_GAP);
		remove_menu_item((CONTAINER *)pObj,(POBJECT_HEAD)&wota_txt6,CON_H + CON_GAP);
		ota_set_menu=TRUE;
		#endif            
		break;
	#ifdef NEW_MANUAL_OTA
	case EVN_POST_OPEN:
		win_compopup_init(WIN_POPUP_TYPE_SMSG);                        
		win_compopup_set_frame(200, 200, 200, 100);
		win_compopup_set_msg("Scan ota...", NULL, 0);
		win_compopup_open_ext(&back_saved);
		bootup = ota_stby_search(&et);
      	break;
	#endif
	case EVN_POST_DRAW:
		#ifndef NEW_MANUAL_OTA
		//win_signal_open(pObj);
		#if Maker_GUI_ON
		win_signal_open_ex(pObj,pObj->frame.uLeft,pObj->frame.uTop + LIST_ITEMCNT*(CON_H + CON_GAP) +20+30);
		#else
		win_signal_open_ex(pObj,pObj->frame.uLeft,pObj->frame.uTop + LIST_ITEMCNT*(CON_H + CON_GAP) +20);
		#endif
		#endif
        //if(cur_front_type == FRONTEND_TYPE_S)
        win_otaset_turn_frntend_ex(cur_front_type);        
		api_stop_timer(&ota_turn_timer_id);
		ota_turn_timer_id = api_start_timer(OTA_TURN_TIMER_NAME, OTA_TURN_TIMER_TIME, win_ota_tpturn_handler);
		//win_signal_refresh();
		//win_signal_update();
		break;
	case EVN_PRE_CLOSE:
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;
	case EVN_POST_CLOSE: 
		//dm_set_onoff(DM_NIMCHECK_ON);
		api_stop_timer(&ota_turn_timer_id);
		#ifdef NEW_MANUAL_OTA
        ota_set_menu=FALSE;
		#endif
		break;
	case EVN_MSG_GOT:
		if(param1 == CTRL_MSG_SUBTYPE_STATUS_SIGNAL)
		{
			#ifndef NEW_MANUAL_OTA
			win_signal_refresh_combo(cur_front_type);
			//win_signal_update();	
			#endif
		}
		else if(param1 == CTRL_MSG_SUBTYPE_CMD_TP_TURN)
		{
			win_otaset_turn_frntend_ex(cur_front_type);
		}
		break;		
		
	}

	return ret;
	
}

//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)

static void win_otaset_load_default_setting(BOOL checkonly,UINT32 fe_type)
{
	UINT8 sat_pos;	
    UINT32 sat_num;
    UINT32 tp_num;
    UINT32 tp_pos;
    UINT32 i,j;
    S_NODE s_node;
    T_NODE t_node;

    if(fe_type == FRONTEND_TYPE_S)
    {
    	sat_num = get_tuner_sat_cnt(OTA_TUNER_IDX);

    	// When enter tp list window, the tp view have been switch to VIEW_SINGLE_SAT.
    	// switch it to VIEW_ALL.
    	recreate_tp_view(VIEW_ALL ,0);

        SAT_NUM = sat_num;
    	SAT_POS = 0;


    	sat_pos = 0xFF;
    	tp_pos = INVALID_POS_NUM;

    	if(!checkonly)
    	{
    		for(i=0;i<sat_num;i++)
    	    {
    	    	get_tuner_sat(OTA_TUNER_IDX, i, &s_node);

    	        if(s_node.sat_orbit == OTA_DATA_SAT_POS)
    	        {
    	            sat_pos = i;
    	            tp_num = get_tp_num_sat(s_node.sat_id);
    	            for(j=0;j<tp_num;j++)
    	            {
    	                get_tp_at(s_node.sat_id,j,&t_node);
    	                if(  t_node.frq >= (OTA_DATA_TP_FREQ - 3)
    	                    && t_node.frq <= (OTA_DATA_TP_FREQ + 3)
    	                    && t_node.sym >= (OTA_DATA_TP_SYMB - 500)
    	                    && t_node.sym <= (OTA_DATA_TP_SYMB + 500)
    	                    && t_node.pol == OTA_DATA_TP_POL)
    	                    {
    	                        tp_pos = j;
    	                        break;
    	                    }
    	            }
    	            
    	            break;
    	        }
    	    }
    		
    		if(sat_pos == 0xFF)
    			sat_pos = 0;
    		
    	}
    	else
    	{
    		sat_pos = ota_satpos;
    		if(sat_pos >= sat_num)
    			sat_pos = 0;
    		tp_pos = TP_POS ;
    	}
    	SAT_POS = sat_pos;

    	ota_satpos = sat_pos;

    	get_tuner_sat(OTA_TUNER_IDX, sat_pos, &s_node);
    	tp_num = get_tp_num_sat(s_node.sat_id);

    	TP_NUM = tp_num;
    	if(tp_pos >= tp_num)
    		tp_pos = 0;
    	TP_POS = tp_pos;	

    	if(TP_NUM > 0)
    	{
    		 get_tp_at(s_node.sat_id,tp_pos,&t_node);
    		 ota_freq = t_node.frq;
    		 ota_symb = t_node.sym;
    		 ota_pol 	= t_node.pol;
    	}
    	else
    	{

    		ota_freq = OTA_DATA_TP_FREQ;
    		ota_symb = OTA_DATA_TP_SYMB;
    		ota_pol  = OTA_DATA_TP_POL;
    	}

    	ota_pid = OTA_DATA_PID;
    }
    else
    {
        win_otaset_load_default_setting_other(checkonly,fe_type);
    }
}

static void win_otaset_load_default_setting_other(BOOL checkonly,UINT32 fe_type)
{
	INT8 rule[20];
	UINT32 freq, symbol, constellation, freq_def;
	P_NODE p_node;
	T_NODE t_node;
	UINT32 prog_num;
	UINT16 cur_channel;
	UINT8  dot_pos;

	UINT32 channel_number=0,fi=0;
    if(fe_type == FRONTEND_TYPE_T || fe_type == FRONTEND_TYPE_ISDBT)
    {
#ifdef AUTO_OTA
    	union ssu_info *ssu = (union ssu_info*)MALLOC(sizeof(union ssu_info));
    	si_get_ota_ssuinfo_t(ssu);
    	if (ssu->t_ssuinfo.frequency != 0)
    	{
    		ota_freq =ssu->t_ssuinfo.frequency;
    		ota_bandwidth = ssu->t_ssuinfo.bandwidth/1000 - 6;
    		ota_pid = ssu->t_ssuinfo.ssu_pid;
    	}
    	else
#endif
    	{
    		ota_freq = OTA_DATA_TP_FREQ_T;
    		ota_bandwidth = OTA_DATA_TP_BW;
    		ota_pid = OTA_DATA_PID;
    	}
#ifdef AUTO_OTA
    	FREE(ssu);
#endif
    }
    else if(fe_type == FRONTEND_TYPE_C)
    {
#ifdef DVBC_SUPPORT        
    	SYSTEM_DATA*	sys_data;
    	sys_data = sys_data_get();

        if(sys_data->current_ft_count > 0)
        {
    	    ota_symb = sys_data->current_ft[0].c_param.sym;
    	    ota_modulation = sys_data->current_ft[0].c_param.constellation;
        }

    	get_default_bandparam(sys_data->country, (Band_param *)&uCountryParam);

    	prog_num = get_prog_num(VIEW_ALL | TV_CHAN | RADIO_CHAN , 0);
    	if (0 != prog_num)
    	{
    		get_prog_at( sys_data_get_cur_group_cur_mode_channel(),&p_node);
    		get_tp_by_id(p_node.tp_id,&t_node);

    		if(t_node.ft_type==FRONTEND_TYPE_S) 
            {
    		ota_freq = uCountryParam[0].start_freq;
    		ota_symb = sys_data->current_ft[0].c_param.sym;
    		ota_modulation= sys_data->current_ft[0].c_param.constellation;
            }
			else if(t_node.ft_type==FRONTEND_TYPE_C) 
			{
			ota_freq = t_node.frq;
    		ota_symb = t_node.sym;
    		ota_modulation= t_node.FEC_inner;
			}
    	}
        else
        {
    		ota_freq = uCountryParam[0].start_freq;
    		ota_symb = sys_data->current_ft[0].c_param.sym;
    		ota_modulation= sys_data->current_ft[0].c_param.constellation;
    	}
    	ota_pid = OTA_DATA_PID;
#endif            
    }

}
#ifdef AUTO_OTA 
static void win_auto_otaset_load_default_setting_other(BOOL checkonly,UINT32 fe_type)
{
	INT8 rule[20];
	UINT32 freq, symbol, constellation, freq_def;
	P_NODE p_node;
	T_NODE t_node;
	UINT32 prog_num;
	UINT16 cur_channel;
	UINT8  dot_pos;

	UINT32 channel_number=0,fi=0;
    if(fe_type == FRONTEND_TYPE_T || fe_type == FRONTEND_TYPE_ISDBT)
    {

	
		union ssu_info *ssu = (union ssu_info*)MALLOC(sizeof(union ssu_info));
    	si_get_ota_ssuinfo_t(ssu);
    	if (ssu->t_ssuinfo.frequency != 0)
    	{
    		ota_freq =ssu->t_ssuinfo.frequency;
    		ota_bandwidth = ssu->t_ssuinfo.bandwidth/1000 - 6;
    		ota_pid = ssu->t_ssuinfo.ssu_pid;
    	}
    	else


    	{
    		ota_freq = OTA_DATA_TP_FREQ_T;
    		ota_bandwidth = OTA_DATA_TP_BW;
    		ota_pid = OTA_DATA_PID;
    	}

    	FREE(ssu);
	

    }
    else if(fe_type == FRONTEND_TYPE_C)
    {
#ifdef DVBC_SUPPORT   
		union ssu_info *ssu = (union ssu_info*)MALLOC(sizeof(union ssu_info));
    	si_get_ota_ssuinfo_c(ssu);
    	if (ssu->t_ssuinfo.frequency != 0)
    	{
    		ota_freq =ssu->c_ssuinfo.frequency;
    		ota_symb= ssu->c_ssuinfo.symbol;
			ota_modulation= ssu->c_ssuinfo.qam;
    		ota_pid = ssu->c_ssuinfo.ssu_pid;
    	}
    	else


    	{
    		ota_freq = OTA_DATA_TP_FREQ_T;
    		ota_symb= OTA_DATA_TP_SYMB;
			ota_modulation= OTA_DATA_MODULATION;
    		ota_pid = OTA_DATA_PID;
    	}

    	FREE(ssu);
#endif 
    }

}

void win_auto_otaset_load_default_setting(BOOL checkonly,UINT32 fe_type)
{
	
    S_NODE s_node;
    T_NODE t_node;

    if(fe_type == FRONTEND_TYPE_S)
    {
	#ifdef DVBS_SUPPORT  
		
		union ssu_info *ssu = (union ssu_info*)MALLOC(sizeof(union ssu_info));
    	si_get_ota_ssuinfo_s(ssu);
		
    	if (ssu->s_ssuinfo.frequency != 0)
    	{
    		ota_freq =ssu->s_ssuinfo.frequency;
    		ota_symb= ssu->s_ssuinfo.symbol;
			ota_pol= ssu->s_ssuinfo.polar;
    		ota_pid = ssu->s_ssuinfo.ssu_pid;
			ota_presat_id=ssu->s_ssuinfo.ssu_sat_id;
		
			
    	}
		else
    	{
    		ota_freq = OTA_DATA_TP_FREQ;
    		ota_symb= OTA_DATA_TP_SYMB;
			ota_pol= OTA_DATA_TP_POL;
    		ota_pid = OTA_DATA_PID;
    	}
    	FREE(ssu);
    
    	
    #endif	
	
	
    }
    else
    {
        win_auto_otaset_load_default_setting_other(checkonly,fe_type);
    }
	if(cur_front_type==FRONTEND_TYPE_S||cur_front_type==FRONTEND_TYPE_C)
	{
	m_ota_cfg.t_node.frq = ota_freq;
	m_ota_cfg.t_node.sym= ota_symb;
	m_ota_cfg.t_node.pol = ota_pol;
	m_ota_cfg.t_node.FEC_inner= ota_modulation;
	m_ota_cfg.pid = ota_pid;
	get_sat_by_id(ota_presat_id, &s_node);
    MEMCPY(&m_ota_cfg.s_node, &s_node, sizeof(s_node));
	}
	if(cur_front_type==FRONTEND_TYPE_T||cur_front_type==FRONTEND_TYPE_ISDBT)
	{
	
	m_ota_cfg.t_node.frq = ota_freq;
	m_ota_cfg.t_node.bandwidth= ota_bandwidth+6;
	m_ota_cfg.pid = ota_pid;
	
	}
}
#endif
static void win_otaset_load_sat_setting(UINT32 sat_idx, BOOL update)
{
	MULTISEL* msel;
	S_NODE s_node;
	T_NODE t_node;
	
	UINT32 val = 0;
	CONTAINER* sat_item = &wota_con0;
	POBJECT_HEAD pObj;
	UINT32 i,tp_num;

	get_tuner_sat(OTA_TUNER_IDX, sat_idx,&s_node);
	tp_num = get_tp_num_sat(s_node.sat_id);
	msel = &wota_num1;
	OSD_SetMultiselCount(msel, tp_num);
	OSD_SetMultiselSel(msel, 0);/*First TP*/
	
	if(s_node.sat_orbit == OTA_DATA_SAT_POS)/*If default Sat,set to default TP*/
	{
		for(i=0;i<tp_num;i++)
		{
			get_tp_at(s_node.sat_id,i,&t_node);
			if(  t_node.frq >= (OTA_DATA_TP_FREQ - 3)
			    && t_node.frq <= (OTA_DATA_TP_FREQ + 3)
			    && t_node.sym >= (OTA_DATA_TP_SYMB - 500)
			    && t_node.sym <= (OTA_DATA_TP_SYMB + 500)
			    && t_node.pol == OTA_DATA_TP_POL)
				{
					OSD_SetMultiselSel(msel,i);/*Default TP pos*/
					break;
				}
		}
	}

	if(TP_NUM > 0)
	{
		 get_tp_at(s_node.sat_id,TP_POS,&t_node);
		 ota_freq = t_node.frq;
		 ota_symb = t_node.sym;
		 ota_pol 	= t_node.pol;
	}
	else
	{

		ota_freq = OTA_DATA_TP_FREQ;
		ota_symb = OTA_DATA_TP_SYMB;
		ota_pol  = OTA_DATA_TP_POL;
	}

	win_otaset_load_FreqSymbPid_display(cur_front_type);

	if(update)
	{
		pObj = (POBJECT_HEAD)sat_item;
		pObj = OSD_GetObjpNext(pObj);
		while(pObj)
		{
			OSD_DrawObject(pObj, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
			pObj = OSD_GetObjpNext(pObj);
		}
	}
}


static void win_otaset_load_tp_setting(UINT32 tp_idx, BOOL update)
{
	S_NODE s_node;
	T_NODE t_node;
	CONTAINER* tp_item = &wota_con1;
	POBJECT_HEAD pObj;	

	get_tuner_sat(OTA_TUNER_IDX, SAT_POS,&s_node);
	get_tp_at(s_node.sat_id,tp_idx,&t_node);

	ota_freq = t_node.frq;
	ota_symb = t_node.sym;
	ota_pol  = t_node.pol;

	win_otaset_load_FreqSymbPid_display(cur_front_type);
	
	if(update)
	{
		pObj = (POBJECT_HEAD)tp_item;
		pObj = OSD_GetObjpNext(pObj);
		while(pObj)
		{
			OSD_DrawObject(pObj, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
			pObj = OSD_GetObjpNext(pObj);
		}
	}

}
void win_otaset_load_FreqSymbPid_display(UINT32 fe_type)
{
	EDIT_FIELD* edf;
	MULTISEL*  msel;

    if(fe_type == FRONTEND_TYPE_S)
    {
    	edf = &wota_num2;
    	OSD_SetEditFieldContent(edf, STRING_NUMBER, (UINT32)ota_freq);

    	
    	edf = & wota_num3;
    	OSD_SetEditFieldContent(edf, STRING_NUMBER, (UINT32)ota_symb);
    	
    	edf = &wota_num5;
    	OSD_SetEditFieldContent(edf, STRING_NUMBER, (UINT32)ota_pid);

    	msel = &wota_num4;
    	OSD_SetMultiselSel(msel, ota_pol);
    }
    else
    {
        win_otaset_load_FreqSymbPid_display_other(fe_type);
    }
}

INT32 win_otaset_get_FreqSymbPid_display(UINT32 fe_type)
{
	EDIT_FIELD* edf;
	MULTISEL*  msel;
	UINT32 val;
	INT32 ret = 0;
    if(fe_type == FRONTEND_TYPE_S)
    {
    	edf = &wota_num2;
    	OSD_GetEditFieldIntValue(edf, &val);
    	if(ota_freq != val)
    	{
    		ota_freq = val;
    		ret = -1;
    	}
    	
    	edf = & wota_num3;
    	OSD_GetEditFieldIntValue(edf, &val);
    	if(ota_symb != val)
    	{
    		ota_symb = val;
    		ret = -1;
    	}
    	
    	edf = &wota_num5;
    	OSD_GetEditFieldIntValue(edf, &val);
    	ota_pid = val;

    	msel = &wota_num4;
    	val = OSD_GetMultiselSel(msel);
    	if(ota_pol != val)
    	{
    		ota_pol = val;
    		ret = -1;
    	}
    		

    	msel = &wota_num0;
    	ota_satpos = OSD_GetMultiselSel(msel);
    	m_ota_cfg.pid = ota_pid;

    	return ret;
    }
    else
    {
        win_otaset_get_FreqSymbPid_display_other(fe_type);
    }
}

void win_otaset_turn_frntend(void)
{
	UINT8 back_saved;
	S_NODE s_node;
	T_NODE t_node;
	
	win_otaset_get_FreqSymbPid_display(cur_front_type);

	cur_tuner_idx = OTA_TUNER_IDX;

#ifdef _BUILD_OTA_E_
	MEMCPY(&s_node, &m_ota_cfg.s_node, sizeof(s_node));
#else
	get_tuner_sat(OTA_TUNER_IDX, ota_satpos, &s_node);

	MEMCPY(&m_ota_cfg.s_node, &s_node, sizeof(s_node));
	t_node.frq = ota_freq;
	t_node.sym = ota_symb;
	t_node.pol = ota_pol;
	MEMCPY(&m_ota_cfg.t_node, &t_node, sizeof(t_node));
	m_ota_cfg.pid = ota_pid;
    
#endif	

#ifndef NEW_DEMO_FRAME
	struct xpond_antenna_info	xai;
	
	sat2antenna_ext(&s_node, &xai.antenna,OTA_TUNER_IDX);
	xai.xpond.frq = ota_freq;
	xai.xpond.sym = ota_symb;
	xai.xpond.pol = ota_pol;
	UIChChgSetAntennaXpond(&xai);
#else
	struct ft_antenna antenna;
	union ft_xpond xponder;
	struct nim_device *nim = NULL;
	
	cur_tuner_idx = s_node.tuner1_valid ? 0 : 1;
	MEMSET(&antenna, 0, sizeof(struct ft_antenna));
	MEMSET(&xponder, 0, sizeof(union ft_xpond));

//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
  #ifdef DVBS_SUPPORT
	sat2antenna(&s_node, &antenna);	
  #endif
//#endif

    antenna.antenna_enable = 1;
	xponder.s_info.type = FRONTEND_TYPE;
	xponder.s_info.frq = ota_freq;
	xponder.s_info.sym = ota_symb;
	xponder.s_info.pol = ota_pol;
	nim = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, (s_node.tuner1_valid ? 0 : (s_node.tuner2_valid ? 1 : 0)));
	frontend_tuning(nim, &antenna, &xponder, 1);
    if(0 == cur_tuner_idx) //TUNER1 <--> TSI_TS_A <--> DMX0
    {
        tsi_dmx_src_select(TSI_DMX_0,TSI_TS_A);
        tsi_select(TSI_TS_A,ts_route_get_nim_tsiid(0));
    }
    else//TUNER2 <--> TSI_TS_B <--> DMX0
    {
        tsi_dmx_src_select(TSI_DMX_0,TSI_TS_B);
        tsi_select(TSI_TS_B, ts_route_get_nim_tsiid(1));
    }
#endif
	
	wincom_dish_move_popup_open(ota_presat_id,s_node.sat_id,cur_tuner_idx,&back_saved);
	ota_presat_id = s_node.sat_id;
}

void win_otaset_turn_frntend_ex(UINT32 fe_type)
{
	UINT8 back_saved;
	S_NODE s_node;
	T_NODE t_node;
	struct ft_antenna antenna;
	union ft_xpond xponder;
	struct nim_device *nim = NULL;
    UINT32 Frequency=0;	
    INT32  nim_id = 0;
    
    
    win_otaset_get_FreqSymbPid_display(fe_type);
    if(FRONTEND_TYPE_S == fe_type)
    {
    	cur_tuner_idx = OTA_TUNER_IDX;
#ifdef _BUILD_OTA_E_
    	MEMCPY(&s_node, &m_ota_cfg.s_node, sizeof(s_node));
#else
    	get_tuner_sat(OTA_TUNER_IDX, ota_satpos, &s_node);
    	MEMCPY(&m_ota_cfg.s_node, &s_node, sizeof(s_node));
    	t_node.frq = ota_freq;
    	t_node.sym = ota_symb;
    	t_node.pol = ota_pol;
    	MEMCPY(&m_ota_cfg.t_node, &t_node, sizeof(t_node));
    	m_ota_cfg.pid = ota_pid;        
#endif	
    	cur_tuner_idx = s_node.tuner1_valid ? 0 : 1;
    	MEMSET(&antenna, 0, sizeof(struct ft_antenna));
    	MEMSET(&xponder, 0, sizeof(union ft_xpond));
    #ifdef DVBS_SUPPORT
    	sat2antenna(&s_node, &antenna);	
    #endif

        antenna.antenna_enable = 1;
    	xponder.s_info.type = fe_type;
    	xponder.s_info.frq = ota_freq;
    	xponder.s_info.sym = ota_symb;
    	xponder.s_info.pol = ota_pol;
    	nim = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, (s_node.tuner1_valid ? 0 : (s_node.tuner2_valid ? 1 : 0)));
    	frontend_tuning(nim, &antenna, &xponder, 1);
        if(0 == cur_tuner_idx) //TUNER1 <--> TSI_TS_A <--> DMX0
        {
            tsi_dmx_src_select(TSI_DMX_0,TSI_TS_A);
            tsi_select(TSI_TS_A,ts_route_get_nim_tsiid(0));
        }
        else//TUNER2 <--> TSI_TS_B <--> DMX0
        {
            tsi_dmx_src_select(TSI_DMX_0,TSI_TS_B);
            tsi_select(TSI_TS_B, ts_route_get_nim_tsiid(1));
        }    	
    	wincom_dish_move_popup_open(ota_presat_id,s_node.sat_id,cur_tuner_idx,&back_saved);
    	ota_presat_id = s_node.sat_id;
    }
    else if(FRONTEND_TYPE_T == fe_type || FRONTEND_TYPE_ISDBT == fe_type)
    {
        //board config isdbt and dvbt is same.
        if(FRONTEND_TYPE_T == board_get_frontend_type(0))
        {
            nim = dev_get_by_id(HLD_DEV_TYPE_NIM, 0);
            nim_id = 0; 
        }
        else if(FRONTEND_TYPE_T == board_get_frontend_type(1))
        {
            nim = dev_get_by_id(HLD_DEV_TYPE_NIM, 1);
            nim_id = 1;
        }
        
        Frequency  = ota_freq;   
    	t_node.frq = ota_freq;
    	t_node.bandwidth = (ota_bandwidth + 6);
        
    	MEMCPY(&m_ota_cfg.t_node, &t_node, sizeof(t_node));
    	m_ota_cfg.pid = ota_pid;
        
    	if(NULL != nim)
    	{
        	MEMSET(&xponder, 0, sizeof(xponder));
        	xponder.t_info.type = FRONTEND_TYPE_T;
        	xponder.t_info.frq = Frequency;
        	xponder.t_info.band_width = (ota_bandwidth + 6)*1000;//kHZ
            xponder.t_info.usage_type = (UINT8)USAGE_TYPE_AUTOSCAN;
    		//frontend_set_nim(nim, NULL, &xpond, 1);
    		//modify for ISDBT project,in ota_upgrade menu, sometime system reset 2011 11 26
    		if(FRONTEND_TYPE_ISDBT == fe_type)
		{
    			if(47000<=xponder.t_info.frq &&900000>=xponder.t_info.frq)
    				frontend_tuning(nim, NULL, &xponder, 1);
		}
		else
			frontend_tuning(nim, NULL, &xponder, 1);
    	}

    }
    else if(FRONTEND_TYPE_C == fe_type)
    {      
        if(fe_type == (UINT32)board_get_frontend_type(0))
        {
            nim = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 0);
		    nim_id=0;
        }
        else if(fe_type == (UINT32)board_get_frontend_type(1))
        {
            nim = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 1);
			nim_id=1;
        }

    	t_node.frq = ota_freq;
    	t_node.sym = ota_symb;
    	t_node.pol = ota_pol;
    	t_node.FEC_inner = ota_modulation;
    	MEMCPY(&m_ota_cfg.t_node, &t_node, sizeof(t_node));
    	m_ota_cfg.pid = ota_pid;
    	if(NULL != nim)
    	{
        	MEMSET(&xponder, 0, sizeof(xponder));
        	xponder.c_info.type = fe_type;
        	xponder.c_info.frq = ota_freq;
        	xponder.c_info.sym = ota_symb;
        	xponder.c_info.modulation = ota_modulation;
    		//frontend_set_nim(nim, NULL, &xpond, 1);
    		frontend_tuning(nim, NULL, &xponder, 1);
    	}
    }
    if(FRONTEND_TYPE_S != fe_type)
    {
        if(0 == nim_id) //TUNER1 <--> TSI_TS_A <--> DMX0
        {
            tsi_dmx_src_select(TSI_DMX_0,TSI_TS_A);
            tsi_select(TSI_TS_A,ts_route_get_nim_tsiid(0));
        }
        else//TUNER2 <--> TSI_TS_B <--> DMX0
        {
            tsi_dmx_src_select(TSI_DMX_0,TSI_TS_B);
            tsi_select(TSI_TS_B, ts_route_get_nim_tsiid(1));
        } 
    }
}

//#else



void win_otaset_load_FreqSymbPid_display_other(UINT32 fe_type)
{
	EDIT_FIELD* edf;
	MULTISEL*  msel;
    UINT16 freq_def, symb_def;
    UINT8 dot_pos;
    int sel;

	/* FREQ */
	edf = &wota_num2;
	wincom_mbs_to_i_with_dot(otaset_display_strs[0], &freq_def, &dot_pos);
	if(freq_def==0)
		wincom_i_to_mbs_with_dot(otaset_display_strs[0],
	                         ota_freq, DVBC_OTA_EDIT_LEN - 1/*without dot*/, 0x02/*xxx.xx*/);
	OSD_SetEditFieldSuffix(edf, STRING_ANSI, (UINT32)" KHz");
	OSD_SetEditFieldStyle(edf,NORMAL_EDIT_MODE | SELECT_STATUS);
    
    if(fe_type == FRONTEND_TYPE_C)
    {
#ifdef DVBC_SUPPORT
        /* FREQ */
    	edf = &wota_num2;
    	wincom_mbs_to_i_with_dot(otaset_display_strs[0], &freq_def, &dot_pos);
    	//if(freq_def==0)
    		wincom_i_to_mbs_with_dot(otaset_display_strs[0],
    	                         ota_freq, DVBC_OTA_EDIT_LEN - 1/*without dot*/, 0x02/*xxx.xx*/);
    	OSD_SetEditFieldSuffix(edf, STRING_ANSI, (UINT32)" MHz");
    	OSD_SetEditFieldStyle(edf,NORMAL_EDIT_MODE | SELECT_STATUS);

    	/* SYMBOL */
    	edf = & wota_num3;
    	OSD_SetEditFieldSuffix(edf, STRING_ANSI, (UINT32)" KBps");
    	wincom_i_to_mbs(otaset_display_strs[1],
    	                ota_symb, DVBC_OTA_EDIT_LEN - 1);
    	OSD_SetEditFieldStyle(edf,NORMAL_EDIT_MODE | SELECT_STATUS);        
        /* QAM */
    	msel = &wota_num4;
        sel = ota_modulation - QAM16;
        sel = sel > 0 ? sel : 0;
        OSD_SetMultiselSel(msel, sel);
#endif        
        
    }
    else if(fe_type == FRONTEND_TYPE_ISDBT || fe_type == FRONTEND_TYPE_T)
    {
        /*freq*/
    	edf = &wota_num2;
    	OSD_SetEditFieldContent(edf, STRING_NUMBER, (UINT32)ota_freq);
        
        /*band*/
	    msel = &wota_num4;
	    OSD_SetMultiselSel(msel, ota_bandwidth);     
    }
    
    /* PID */
	edf = &wota_num5;
	OSD_SetEditFieldContent(edf, STRING_NUMBER, (UINT32)ota_pid);

}

INT32 win_otaset_get_FreqSymbPid_display_other(UINT32 fe_type)
{
	EDIT_FIELD* edf;
	MULTISEL*  msel;
	UINT32 val;
	INT32 ret = 0;
	UINT8	dot_pos;
    UINT32 freq, symbol, constellation,bandwith;
    if(fe_type == FRONTEND_TYPE_C)
    {
    	/* FREQ */
    	edf = &wota_num2;
        wincom_mbs_to_i_with_dot(otaset_display_strs[0], &freq, &dot_pos);
    	//OSD_GetEditFieldIntValue(edf, &val);
    	if(ota_freq != freq)
    	{
    		ota_freq = freq;
    		ret = -1;
    	}
    	
    	/* SYMBOL */
    	edf = & wota_num3;
        symbol = wincom_mbs_to_i(otaset_display_strs[1]);
    	//OSD_GetEditFieldIntValue(edf, &val);
    	if(ota_symb != symbol)
    	{
    		ota_symb = symbol;
    		ret = -1;
    	}

        /* QAM */
    	msel = &wota_num4;
    	constellation = OSD_GetMultiselSel(msel)+QAM16;
    	if(ota_modulation!= constellation)
    	{
    		ota_modulation = constellation;
    		ret = -1;
    	}
    }	
	else
	{
    	edf = &wota_num2;
    	OSD_GetEditFieldIntValue(edf, &val);
    	if(ota_freq != val)
    	{
    		ota_freq = val;
    		ret = -1;
    	}
        
    	msel = &wota_num4;
    	val = OSD_GetMultiselSel(msel);
        
        if(ota_bandwidth != val)
    	{
    		ota_bandwidth = val;
    		ret = -1;
    	}      
	}
    /* PID */
	edf = &wota_num5;
	OSD_GetEditFieldIntValue(edf, &val);
	ota_pid = val;
    m_ota_cfg.pid = ota_pid;

	return ret;
}
/*
void win_otaset_turn_frntend(void)
{
	UINT8 back_saved;
	S_NODE s_node;
	T_NODE t_node;
	struct nim_device *nim;
	union ft_xpond xpond;
    

	nim = dev_get_by_id(HLD_DEV_TYPE_NIM, 0);

	t_node.frq = ota_freq;
	t_node.sym = ota_symb;
	t_node.pol = ota_pol;
	t_node.FEC_inner = ota_modulation;
	MEMCPY(&m_ota_cfg.t_node, &t_node, sizeof(t_node));
	m_ota_cfg.pid = ota_pid;
    
	win_otaset_get_FreqSymbPid_display();

	if(NULL != nim)
	{
    	MEMSET(&xpond, 0, sizeof(xpond));
    	xpond.c_info.type = FRONTEND_TYPE_C;
    	xpond.c_info.frq = ota_freq;
    	xpond.c_info.sym = ota_symb;
    	xpond.c_info.modulation = ota_modulation;
		//frontend_set_nim(nim, NULL, &xpond, 1);
		frontend_tuning(nim, NULL, &xpond, 1);
	}


}
*/

//#endif
POBJECT_HEAD win_otaset_get_child_obj(POBJECT_HEAD root, UINT16 obj_type)
{
	OBJLIST *ol;
    CONTAINER* con;
    POBJECT_HEAD oh,pobj = NULL;
    INT16  i,cnt;     
    
    if(OT_CONTAINER == root->bType)
    {
		con = (CONTAINER*)root;
		oh = (POBJECT_HEAD )(con->pNextInCntn);
        while(oh)
        {
            if(oh->bType == obj_type)
            {
                pobj = oh;
                break;
            }
            oh = oh->pNext;
        }        
    }
    else if(OT_OBJLIST == root->bType)
    {
		ol = (OBJLIST*)root;
		cnt = OSD_GetObjListCount(ol);
		for(i = 0; i < cnt; i++)
		{
			oh = ol->pListField[i];
			if(oh!=NULL && oh->bType == obj_type)
			{
                pobj = oh;
                break;
			}
		}        
    }

    return pobj;
}

static BOOL win_otaset_menu_config_dvbs(UINT32 fe_type)
{
    POBJECT_HEAD    pobj;    
    BOOL            ret = TRUE;
    UINT16          i = 0;

    if(FRONTEND_TYPE_S == fe_type)
    {
        for(i = 0; i < sizeof(otaset_dvbs_menu)/sizeof(otaset_dvbs_menu[0]); i++)
        {
            pobj = otaset_items[i];
            OSD_MoveObject(pobj,otaset_dvbs_menu[i].obj_rect.uLeft,otaset_dvbs_menu[i].obj_rect.uTop,0);
            OSD_SetAttr(pobj,otaset_dvbs_menu[i].obj_active);
            switch(i)
            {
                case 2:
                    {
                    PEDIT_FIELD  pedit;
                    pedit = (PEDIT_FIELD)win_otaset_get_child_obj(pobj,OT_EDITFIELD);
                    if(pedit != NULL)
                        pedit->pcsPattern = fre_pat;
                    }
                    break;
                case 3:
                    {
                    PEDIT_FIELD  pedit;
                    pedit = (PEDIT_FIELD)win_otaset_get_child_obj(pobj,OT_EDITFIELD);
                    if(pedit != NULL)
                        pedit->pcsPattern = symb_pat;
                    }                    
                    break;
                case 4:
                    {
                        PMULTISEL psel;
                        PTEXT_FIELD ptxt;
                        psel = (PMULTISEL)win_otaset_get_child_obj(pobj,OT_MULTISEL); 
                        if(psel != NULL)
                        {
                            psel->nCount = 2;
                            psel->pSelTable = tp_polarity_ids;
                        }
                        ptxt = (PTEXT_FIELD)win_otaset_get_child_obj(pobj,OT_TEXTFIELD);
                        if(ptxt != NULL)
                            ptxt->wStringID = otaset_dvbs_menu[i].obj_strid;
                    }
                    break;                    
                default:
                    break;
            }
        }
    }
    else
        ret = FALSE;
    return ret;
}

static BOOL win_otaset_menu_config_dvbt(UINT32 fe_type)
{
    POBJECT_HEAD    pobj;    
    BOOL            ret = TRUE;
    UINT16          i = 0;

    if(FRONTEND_TYPE_T == fe_type || FRONTEND_TYPE_ISDBT == fe_type)
    {
        for(i = 0; i < sizeof(otaset_dvbt_menu)/sizeof(otaset_dvbt_menu[0]); i++)
        {
            pobj = otaset_items[i];
            OSD_MoveObject(pobj,otaset_dvbt_menu[i].obj_rect.uLeft,otaset_dvbt_menu[i].obj_rect.uTop,0);
            OSD_SetAttr(pobj,otaset_dvbt_menu[i].obj_active);
            switch(i)
            {
                case 2:
                    {
                    PEDIT_FIELD  pedit;
                    pedit = (PEDIT_FIELD)win_otaset_get_child_obj(pobj,OT_EDITFIELD);
                    if(pedit != NULL)
                        pedit->pcsPattern = fre_pat_dvbt;
                    }
                    break;
                case 3:
                    {
                    PEDIT_FIELD  pedit;
                    pedit = (PEDIT_FIELD)win_otaset_get_child_obj(pobj,OT_EDITFIELD);
                    if(pedit != NULL)
                        pedit->pcsPattern = symb_pat_dvbt;
                    }                    
                    break;
                case 4:
                    {
                        PMULTISEL psel;
                        PTEXT_FIELD ptxt;
                        psel = (PMULTISEL)win_otaset_get_child_obj(pobj,OT_MULTISEL); 
                        if(psel != NULL)
                        {
                            psel->nCount = 3;
                            psel->pSelTable = bandwidth_ids;
                        }
                        ptxt = (PTEXT_FIELD)win_otaset_get_child_obj(pobj,OT_TEXTFIELD);
                        if(ptxt != NULL)
                            ptxt->wStringID = otaset_dvbt_menu[i].obj_strid;
                    }
                    break;                      
                default:
                    break;
            }
        }
    }
    else
        ret = FALSE;
    return ret;
}

static BOOL win_otaset_menu_config_dvbc(UINT32 fe_type)
{
    POBJECT_HEAD    pobj;    
    BOOL            ret = TRUE;
    UINT16          i = 0;

    if(FRONTEND_TYPE_C == fe_type)
    {
        for(i = 0; i < sizeof(otaset_dvbc_menu)/sizeof(otaset_dvbc_menu[0]); i++)
        {
            pobj = otaset_items[i];
            OSD_MoveObject(pobj,otaset_dvbc_menu[i].obj_rect.uLeft,otaset_dvbc_menu[i].obj_rect.uTop,0);
            OSD_SetAttr(pobj,otaset_dvbc_menu[i].obj_active);
            switch(i)
            {
                case 2:
                    {
                    PEDIT_FIELD  pedit;
                    pedit = (PEDIT_FIELD)win_otaset_get_child_obj(pobj,OT_EDITFIELD);
                    if(pedit != NULL)
                        pedit->pcsPattern = fre_pat_dvbc;
                    }
                    break;
                case 3:
                    {
                    PEDIT_FIELD  pedit;
                    pedit = (PEDIT_FIELD)win_otaset_get_child_obj(pobj,OT_EDITFIELD);
                    if(pedit != NULL)
                        pedit->pcsPattern = symb_pat_dvbc;
                    }                    
                    break; 
                case 4:
                    {
                        PMULTISEL psel;
                        PTEXT_FIELD ptxt;
                        psel = (PMULTISEL)win_otaset_get_child_obj(pobj,OT_MULTISEL); 
                        if(psel != NULL)
                        {
                            psel->nCount = 5;
                            psel->pSelTable = ota_model_type_ids;
                        }
                        ptxt = (PTEXT_FIELD)win_otaset_get_child_obj(pobj,OT_TEXTFIELD);
                        if(ptxt != NULL)
                            ptxt->wStringID = otaset_dvbc_menu[i].obj_strid;
                    }
                    break;                         
                    
                default:
                    break;
            }
        }
    }
    else
        ret = FALSE;
    return ret;
}

void win_otaset_menu_config(UINT32 fe_type)
{
    if(fe_type == FRONTEND_TYPE_S)
        win_otaset_menu_config_dvbs(fe_type);
    else if(fe_type == FRONTEND_TYPE_T||fe_type == FRONTEND_TYPE_ISDBT)
        win_otaset_menu_config_dvbt(fe_type);        
    else if(fe_type == FRONTEND_TYPE_C)   
        win_otaset_menu_config_dvbc(fe_type);        
}

void win_signal_refresh_combo(UINT32 fe_type)
{    
    UINT16 level = 0;
    UINT8 quality = 0;
    UINT8 lock = 0;
	struct nim_device* nim_dev = NULL;
    INT32   cur_tuner;
    S_NODE  s_node;
    
    if(fe_type == FRONTEND_TYPE_S)
    {

#ifdef _BUILD_OTA_E_
    	MEMCPY(&s_node, &m_ota_cfg.s_node, sizeof(s_node));
#else
    	get_tuner_sat(OTA_TUNER_IDX, ota_satpos, &s_node);
#endif
        
        nim_dev = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, (s_node.tuner1_valid ? 0 : (s_node.tuner2_valid ? 1 : 0)));
        
    }
    else if(fe_type == FRONTEND_TYPE_T || fe_type == FRONTEND_TYPE_ISDBT)
    {
        //board config isdbt and dvbt is same.
        if(FRONTEND_TYPE_T == board_get_frontend_type(0))
        {
            nim_dev = dev_get_by_id(HLD_DEV_TYPE_NIM, 0);
        }
        else if(FRONTEND_TYPE_T == board_get_frontend_type(1))
        {
            nim_dev = dev_get_by_id(HLD_DEV_TYPE_NIM, 1);
        }        
    }
    else if(fe_type == FRONTEND_TYPE_C)
    {
        if(fe_type == (UINT32)board_get_frontend_type(0))
            nim_dev = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 0);
        else if(fe_type == (UINT32)board_get_frontend_type(1))
            nim_dev = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 1);        
    }
	nim_get_lock(nim_dev, &lock);
#ifdef _S3281_	
	nim_get_AGC(nim_dev, &level);
#endif
	nim_get_SNR(nim_dev, &quality);
	if(NULL != nim_dev->do_ioctl)
    {
      nim_dev->do_ioctl(nim_dev, NIM_DRIVER_GET_AGC, (UINT32)&level);
    }

    dem_signal_to_display(lock,&level,&quality);
    win_signal_set_level_quality(level,quality,lock);
	win_signal_update();

}
void ota_set_front_type()
{
	cur_front_type=board_get_frontend_type(0);//add by Iris for AUTO OTA
}

